{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9c377d64",
   "metadata": {},
   "source": [
    "## 易错点\n",
    "groupby(by='id',as_index=False) 忘了加as_index\n",
    "p = i[0].split(',') 后面忘了加split(',')\n",
    "apriori 首字母小写，另外注意别忘了use_colnames=True\n",
    "frequent_itemsets.sort_values(by='support',ascending=False,inplace=True) 少了inplace=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f045099e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori # 生产频繁项集\n",
    "from mlxtend.frequent_patterns import association_rules # 生产关联规则\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66a65c57",
   "metadata": {},
   "outputs": [],
   "source": [
    "order_data = pd.read_csv('./data/GoodsOrder_eng.csv',header=0,encoding='gbk')\n",
    "# 转换数据格式\n",
    "order_data['Goods']= order_data['Goods'].apply(lambda x: ',' + x)\n",
    "order_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b791097",
   "metadata": {},
   "source": [
    "1.将 order_data按id 分组求和，并重置索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67d91a07",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "order_data = order_data.groupby('id',as_index=False)['Goods'].sum().reset_index()\n",
    "#由考生填写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "545232e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "order_data['Goods'] = order_data['Goods'].apply(lambda x: [x[1:]]) # 加了[]\n",
    "order_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02a6848a",
   "metadata": {},
   "outputs": [],
   "source": [
    "order_data_list = list(order_data['Goods'])\n",
    "order_data_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69c99348",
   "metadata": {},
   "source": [
    "2.分割商品名为每一个元素。使得 dataset 最终输出为以下格式:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46f9f2fd",
   "metadata": {},
   "source": [
    "列表是最常用的Python数据类型，它可以作为一个方括号内的逗号分隔值出现。\n",
    "列表的数据项不需要具有相同的类型\n",
    "创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19d3e4d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_transaction = []\n",
    "for i in order_data_list:\n",
    "    #由考生填写 \n",
    "    p = i[0].split(',')\n",
    "    #由考生填写\n",
    "    data_transaction.append(p)\n",
    "data_transaction "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94fba5e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSet = data_transaction\n",
    "column_list = []\n",
    "for var in dataSet:\n",
    "    column_list = set(column_list)|set(var)\n",
    "dataSet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d1dacfe",
   "metadata": {},
   "source": [
    "3.遍历 dataSet 中的每一个商品,并将 data 中对应位置的值加1,即购买一次则在相应物品上加 1，使得 data 输出为以下形式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59a65c41",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = pd.DataFrame(np.zeros((len(dataSet),7)),columns=column_list)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "95c0e4ed",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for i in range(len(dataSet)):\n",
    "    for j in dataSet[i]:\n",
    "        #由考生填写\n",
    "        data[j][i] += 1\n",
    "        #由考生填写 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7d5f267",
   "metadata": {},
   "outputs": [],
   "source": [
    "data= data.applymap(lambda x:1 if x>0 else 0)\n",
    "data "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9169acff",
   "metadata": {},
   "source": [
    "4.使用 Apriori 算法从数据中计算频繁项集，并将最小支持度设置为0.02。然后根据支持度倒排序，最后返回频繁项集 frequent_itemsets，使得frequent itemsets 部分输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "230763a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "frequent_itemsets=apriori(data,min_support=0.02,use_colnames=True)\n",
    "frequent_itemsets.sort_values(by='support',ascending=False,inplace=True)\n",
    "#由考生填写\n",
    "frequent_itemsets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12e53015",
   "metadata": {},
   "source": [
    "5.使用 assoeiation_rules 从频繁项集 frequentitemsets中构建关联规则，metric 为'confidence’min_threshold=0.35.对关联规则按 confidence值进行降序排列,并将排序结果存储在association rule中，使得 dfassociation_rule 部分输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a87589b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#由考生填写\n",
    "association_rule =association_rules(df=frequent_itemsets,metric='confidence',min_threshold=0.35,support_only=True)\n",
    "association_rule.sort_values(by='confidence',ascending=False,inplace=True)\n",
    "#由考生填写\n",
    "df_association_rule=pd.DataFrame(association_rule)\n",
    "df_association_rule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15c70674",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PySpark-2.4.5",
   "language": "python",
   "name": "pyspark-2.4.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
